package com.zx.auth.block.controller;

import com.alibaba.fastjson2.JSONObject;
import com.wf.captcha.GifCaptcha;
import com.zx.auth.block.component.LoginService;
import com.zx.auth.block.constant.Constants;
import com.zx.common.tools.id.components.IdGeneratorComponent;
import com.zx.core.param.LoginParam;
import com.zx.core.wrapper.ObjectWrapper;
import com.zx.redis.block.service.RedisBlockService;
import com.zx.validation.annotation.ParameterValidation;
import com.zx.validation.annotation.ParameterValidations;
import com.zx.validation.enums.ValidateRulesEnum;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.awt.*;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;


@RestController
@RefreshScope //支持Nacos的动态刷新功能。
@RequiredArgsConstructor // Lombok注解，为所有final字段生成构造函数
public class LoginController {
    // 使用final字段和@RequiredArgsConstructor替代@Autowired
    private final LoginService loginService;
    private final RedisBlockService redisBlockService;
    private final IdGeneratorComponent idGeneratorComponent;
    @Value("${auth.captchaEnabled}")
    private boolean captchaEnabled;

    /**
     * 验证码图片
     */
    @GetMapping("/captchaImage")
    public ObjectWrapper<Map<String, Object>> captcha(HttpServletRequest request,
                                                      HttpServletResponse response) {
        ObjectWrapper<Map<String, Object>> result = new ObjectWrapper<>();
        if (!captchaEnabled) {
            return result;
        }
        // 三个参数分别为宽、高、位数
        GifCaptcha gifCaptcha = new GifCaptcha(130, 48, 5);
        // 设置系统字体
        gifCaptcha.setFont(new Font("楷体", Font.PLAIN, 38));
        String capText = gifCaptcha.text().toLowerCase();

        // 使用ID生成器组件生成UUID
        String uuid = idGeneratorComponent.generateId();
        redisBlockService.setCacheObject(Constants.CAPTCHA_CODE_KEY + uuid, capText, Constants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES);

        Map<String, Object> map = new HashMap<>();
        map.put("uuid", uuid);
        map.put("captchaEnabled", captchaEnabled);
        map.put("img", gifCaptcha.toBase64());
        result.setData(map);
        return result;
    }

    @PostMapping("/login")
    @ParameterValidations(
            @ParameterValidation(index = 0, desc = "登录参数", rules = {ValidateRulesEnum.OBJECT})
    )
    public ObjectWrapper<JSONObject> login(@RequestBody LoginParam loginParam) {
        return loginService.login(loginParam.getUsername(), loginParam.getPassword(), loginParam.getCaptcha(), loginParam.getUuid());

    }
}